<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="search_from">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="id" placeholder="请输入ID" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">商品名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="title" placeholder="请输入商品名称" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">分类</label>
                            <div class="layui-input-inline">
                                <script type="text/html" template lay-url="{{ layui.setter.apiHost }}/goods/category/select_all" lay-done="layui.form.render();">
                                    <select name="category_id">
                                        <option value=""></option>
                                        {{# layui.each(d.data, function(index, item){ }}
                                        <option value="{{ item.id }}" {{# if(item.children){ }}disabled{{# } }}>{{ item.title }}</option>
                                        {{# if(item.children){ }}
                                        {{# layui.each(item.children, function(index1, item1){ }}
                                        <option value="{{ item1.id }}" {{# if(item1.children){ }}disabled{{# } }}>--{{ item1.title }}</option>
                                        {{# if(item1.children){ }}
                                        {{# layui.each(item1.children, function(index2, item2){ }}
                                        <option value="{{ item2.id }}" {{# if(item2.children){ }}disabled{{# } }}>----{{ item2.title }}</option>
                                        {{# }); }}
                                        {{# } }}
                                        {{# }); }}
                                        {{# } }}
                                        {{# }); }}
                                    </select>
                                </script>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">商家</label>
                            <div class="layui-input-inline">
                                <script type="text/html" template lay-url="{{ layui.setter.apiHost }}/seller/seller/select" lay-done="layui.form.render();">
                                    <select name="seller_id">
                                        <option value=""></option>
                                        {{# layui.each(d.data, function(index, item){ }}
                                        <option value="{{ index }}">{{ item }}</option>
                                        {{# }); }}
                                    </select>
                                </script>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">品牌</label>
                            <div class="layui-input-inline">
                                <script type="text/html" template lay-url="{{ layui.setter.apiHost }}/system/brand/select" lay-done="layui.form.render();">
                                    <select name="brand_id">
                                        <option value=""></option>
                                        {{# layui.each(d.data, function(index, item){ }}
                                        <option value="{{ index }}">{{ item }}</option>
                                        {{# }); }}
                                    </select>
                                </script>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">审核状态</label>
                            <div class="layui-input-inline">
                                <select name="status">
                                    <option value=""></option>
                                    <option value="0">锁定</option>
                                    <option value="1">审核</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">上架状态</label>
                            <div class="layui-input-inline">
                                <select name="shelves_status">
                                    <option value=""></option>
                                    <option value="0">下架</option>
                                    <option value="1">上架</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">是否推荐</label>
                            <div class="layui-input-inline">
                                <select name="is_rem">
                                    <option value=""></option>
                                    <option value="0">否</option>
                                    <option value="1">是</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="search_button">
                                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                            </button>
                        </div>
                    </div>
                </div>

                <script type="text/html" id="toolbar_tmp">
                    <div class="layui-btn-container">
                        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
                        <button class="layui-btn layui-btn-sm" lay-event="status_on">审核</button>
                        <button class="layui-btn layui-btn-sm" lay-event="status_off">锁定</button>
                        <button class="layui-btn layui-btn-sm" lay-event="shelves_status_on">上架</button>
                        <button class="layui-btn layui-btn-sm" lay-event="shelves_status_off">下架</button>
                        <button class="layui-btn layui-btn-sm" lay-event="rem_on">推荐</button>
                        <button class="layui-btn layui-btn-sm" lay-event="rem_off">取消推荐</button>
                        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="del">删除</button>
                    </div>
                </script>

                <div class="layui-card-body">
                    <table id="table_list" lay-filter="table_list"></table>
                    <script type="text/html" id="img_tmp">
                        {{ d.title }}
                        {{# if(d.image != ''){ }}
                        <a href="{{ d.image }}" target="_blank"><img src="{{ layui.helper.image_resize(d.image, 30, 30) }}" width="30" height="30"></a>
                        {{# } }}
                        {{# if(d.is_rem == 1){ }}
                        <span class="layui-badge">推荐</span>
                        {{# } }}
                    </script>
                    <script type="text/html" id="shelves_status_tmp">
                        {{# if(d.shelves_status == 1){ }}
                        <span class="layui-badge layui-bg-green">上架</span>
                        {{# } else { }}
                        <span class="layui-badge">下架</span>
                        {{# } }}
                    </script>
                    <script type="text/html" id="status_tmp">
                        <input type="checkbox" value="{{d.id}}" lay-skin="switch" lay-text="正常|锁定" lay-filter="status_btn" {{# if(d.status== 1){ }}checked{{# } }}>
                    </script>
                    <script type="text/html" id="action_button">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="../../../layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'helper', 'view'], function () {
        var admin = layui.admin,
            table = layui.table,
            form = layui.form,
            view = layui.view,
            helper = layui.helper,
            edit_view_url = 'goods/goods/add',//添加编辑页模板地址
            api_action_url = layui.setter.apiHost + '/goods/goods';

        //表格操作
        table.render({
            elem: '#table_list',
            url: api_action_url,
            toolbar: '#toolbar_tmp',
            cols: [[
                {type: 'checkbox', fixed: 'left'},
                {field: 'id', title: 'ID', width: 80},
                {field: 'title', title: '商品名称', minWidth: 200, toolbar: '#img_tmp'},
                {field: 'category', title: '分类', width: 100},
                {field: 'sku_code', title: '货号', width: 140},
                {field: 'market_price', title: '市场价', width: 100},
                {field: 'sell_price', title: '销售价', width: 100},
                {field: 'shelves_status', title: '上架', width: 80, templet: '#shelves_status_tmp'},
                {field: 'position', title: '排序', width: 80, sort: true, edit: 'text'},
                {field: 'status', title: '状态', width: 100, templet: '#status_tmp'},
                {field: 'created_at', title: '创建时间', width: 170},
                {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#action_button'}
            ]],
            page: true
        });

        //头部工具栏操作
        table.on('toolbar(table_list)', function (obj) {
            var type = obj.event;
            switch (type) {
                case 'add':
                    select_category();
                    break;
                case 'status_on':
                    helper.ajax(api_action_url + '/status', {status: 1});
                    break;
                case 'status_off':
                    helper.ajax(api_action_url + '/status', {status: 0});
                    break;
                case 'shelves_status_on':
                    helper.ajax(api_action_url + '/shelves_status', {status: 1});
                    break;
                case 'shelves_status_off':
                    helper.ajax(api_action_url + '/shelves_status', {status: 0});
                    break;
                case 'rem_on':
                    helper.ajax(api_action_url + '/rem', {is_rem: 1});
                    break;
                case 'rem_off':
                    helper.ajax(api_action_url + '/rem', {is_rem: 0});
                    break;
                case 'del':
                    layer.confirm('确定删除吗', function (index) {
                        helper.ajax(api_action_url + '/delete');
                    });
                    break;
            }
        });

        //监听工具条操作按钮
        table.on('tool(table_list)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'edit':
                    helper.open_window('编辑', api_action_url, edit_view_url, {id: data.id, width: '100%'});
                    break;
                case 'del':
                    layer.confirm('确定删除吗', function (index) {
                        if (helper.ajax(api_action_url + '/delete', {id: data.id}, false)) {
                            obj.del();
                            layer.close(index);
                        }
                    });
                    break;
            }
        });

        //监听锁定操作
        form.on('switch(status_btn)', function (obj) {
            var send_data = {id: this.value, status: obj.elem.checked == true ? 1 : 0};
            helper.ajax(api_action_url + '/status', send_data, false);
        });

        //添加商品前选择分类
        function select_category() {
            admin.popup({
                title: '选择分类'
                , area: ['500px', '500px']
                , id: new Date().getTime()
                , success: function (layero, index) {
                    view(this.id).render('goods/goods/select_category').done(function () {
                        //监听提交
                        form.on('submit(select_category-submit)', function (data) {
                            var field = data.field; //获取提交的字段
                            if (!field.category_id) {
                                layer.msg('请选择分类', {time: 1000});
                            } else {
                                layer.close(index);
                                helper.open_window('添加商品', api_action_url, edit_view_url, {width: '100%'}, {category_id: field.category_id,sku_code: Date.parse(new Date())});
                            }
                        });
                    });
                }
            });
        }
    });
</script>
</body>
</html>